package com.aso114.project.mvp.activity.fragment;

/**
 * 消息界面-我的
 */

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aso114.project.R;
import com.aso114.project.base.BaseFragment;
import com.aso114.project.bean.UserInfoBean;
import com.aso114.project.mvp.LoginSP;
import com.aso114.project.mvp.activity.AboutUsActivity;
import com.aso114.project.mvp.activity.ChoiceExamActivity;
import com.aso114.project.mvp.activity.EeeorExerciseActivity;
import com.aso114.project.mvp.activity.LoginActivity;
import com.aso114.project.mvp.activity.MyCollectActivity;
import com.aso114.project.mvp.activity.PersonDataActivity;
import com.aso114.project.mvp.activity.PracticeHistoryActivity;
import com.aso114.project.mvp.model.CallBack;
import com.aso114.project.mvp.model.CommentModel;
import com.aso114.project.util.Helper;
import com.aso114.project.util.SystemBarHelper;
import com.aso114.project.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class MainThreeFragment extends BaseFragment {


    Unbinder unbinder;
    @BindView(R.id.layout_title_bar_title_tv)
    TextView layoutTitleBarTitleTv;
    @BindView(R.id.setting)
    RelativeLayout setting;
    @BindView(R.id.update)
    RelativeLayout update;
    @BindView(R.id.softcommt)
    RelativeLayout softcommt;
    @BindView(R.id.photo)
    TextView photo;
    @BindView(R.id.nickname)
    TextView nickname;
    @BindView(R.id.aboutus)
    RelativeLayout aboutus;
    @BindView(R.id.headimg)
    ImageView headimg;
    @BindView(R.id.my_collect)
    TextView myCollect;
    @BindView(R.id.my_mistakes)
    TextView myMistakes;
    @BindView(R.id.my_history)
    TextView myHistory;
    Intent intent;
    @BindView(R.id.layout_title_bar_back_iv)
    ImageView layoutTitleBarBackIv;
    @BindView(R.id.my_name)
    TextView myName;
    @BindView(R.id.title_top_ly)
    RelativeLayout titleTopLy;
    @BindView(R.id.head)
    RelativeLayout head;
    @BindView(R.id.lin1)
    LinearLayout lin1;
    @BindView(R.id.lin2)
    LinearLayout lin2;
    @BindView(R.id.my_exam)
    TextView myExam;
    @BindView(R.id.list_item)
    LinearLayout listItem;
    @BindView(R.id.set)
    TextView set;

    private RequestOptions mRequestOptions;

    @Override
    protected int getLayoutResourceId() {

        return R.layout.fragmentthree;
    }

    @Override
    protected void initView() {
        SystemBarHelper.immersiveStatusBar(getActivity(), 0);
        SystemBarHelper.setHeightAndPadding(getActivity(), titleTopLy);
        layoutTitleBarTitleTv.setText("我  的");
        layoutTitleBarBackIv.setVisibility(View.GONE);

        mRequestOptions = RequestOptions.circleCropTransform()
                .diskCacheStrategy(DiskCacheStrategy.NONE)//不做磁盘缓存
                .skipMemoryCache(true);//不做内存缓存

        if (!TextUtils.isEmpty(LoginSP.getInstance().getToken())) {
            if (!TextUtils.isEmpty(LoginSP.getInstance().getUserName())) {
                myName.setText(LoginSP.getInstance().getUserName());
            } else {
                myName.setText(LoginSP.getInstance().getUserMobile());
            }
            Glide.with(getActivity()).load(LoginSP.getInstance().getUserLitpic()).apply(mRequestOptions.placeholder(R.mipmap.my_touxiang_c).error(R.mipmap.my_touxiang_c)).into(headimg);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void addOnClick() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({R.id.setting, R.id.update, R.id.softcommt, R.id.my_collect, R.id.my_mistakes, R.id
            .my_history, R.id.head, R.id.my_exam, R.id.aboutus})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.head:
                if (TextUtils.isEmpty(LoginSP.getInstance().getToken())) {
                    System.out.println("没有登陆");
                    intent = new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    intent = new Intent(getContext(), PersonDataActivity.class);
                    startActivity(intent);
                }

                break;
            case R.id.my_collect:

                if (TextUtils.isEmpty(LoginSP.getInstance().getToken())) {
                    System.out.println("没有登陆");
                    intent = new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    intent = new Intent(getContext(), MyCollectActivity.class);
                    intent.putExtra("position",0);
                    startActivity(intent);
                }
                break;
            case R.id.my_mistakes:
                if (TextUtils.isEmpty(LoginSP.getInstance().getToken())) {
                    System.out.println("没有登陆");
                    intent = new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    //错题练习
                    intent = new Intent(getContext(), EeeorExerciseActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.my_history:
                if (TextUtils.isEmpty(LoginSP.getInstance().getToken())) {
                    System.out.println("没有登陆");
                    intent = new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    intent = new Intent(getContext(), PracticeHistoryActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.my_exam:
                if (TextUtils.isEmpty(LoginSP.getInstance().getToken())) {
                    System.out.println("没有登陆");
                    intent = new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    intent = new Intent(getContext(), ChoiceExamActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.setting:
                if (TextUtils.isEmpty(LoginSP.getInstance().getToken())) {
                    System.out.println("没有登陆");
                    intent = new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    intent = new Intent(getContext(), PersonDataActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.aboutus:
                intent = new Intent(getContext(), AboutUsActivity.class);//AboutUsActivity ShowActivity
                startActivity(intent);
                break;
            case R.id.update:
                ToastUtils.showToast(getActivity(), "当前已是最新版本");
                break;
            case R.id.softcommt:
                //意见反馈
                try {
                    Uri uri = Uri.parse("market://details?id=" + getActivity().getPackageName());
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                } catch (Exception e) {
                    Helper.showToast("您的手机没有安装Android应用市场");
                    e.printStackTrace();
                }
                break;

        }
    }


    @Override
    public void onResume() {
        super.onResume();
        SystemBarHelper.immersiveStatusBar(getActivity(), 0);
        if (TextUtils.isEmpty(LoginSP.getInstance().getToken())) {
            System.out.println("没有登陆");

            myName.setText("注册/登陆");
            headimg.setImageDrawable(ContextCompat.getDrawable(getActivity(),R.mipmap.home_wode_ic));
        } else {

            CommentModel.getInstant().getUserInfo(LoginSP.getInstance().getToken(), new CallBack() {
                @Override
                public void onSuccess(Object o, boolean isSuccess, String msg) {
                    Helper.gotoLogin(isSuccess);//token失效去重新登陆
                    UserInfoBean loginBean = (UserInfoBean) o;
                    if (loginBean == null) {
                        return;
                    }
                    if (!TextUtils.isEmpty(loginBean.getUserName())) {
                        myName.setText(loginBean.getUserName());
                    } else {
                        myName.setText(loginBean.getUserMobile());
                    }
                    if (!TextUtils.isEmpty((loginBean.getUserLitpic()))) {
                        LoginSP.getInstance().setUserLitpic(loginBean.getUserLitpic());
                        Glide.with(getActivity()).load(loginBean.getUserLitpic()).apply(mRequestOptions.error(R.mipmap.my_touxiang_c)).into(headimg);
                    }else {
                        Glide.with(getActivity()).load(LoginSP.getInstance().getUserLitpic()).apply(mRequestOptions.error(R.mipmap.my_touxiang_c)).into(headimg);
                    }
                    System.out.println("信息====" + loginBean.getUserMobile());
                }

                @Override
                public void onFailure(String fail) {
                    ToastUtils.showToast(getContext(), fail);
                }
            });
        }


    }
}